package com.lht.admin.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *
 * @author cs12110 create at 2019-12-27 23:52
 * <p>
 * @since 1.0.0
 */
@Aspect
@Component
@Slf4j
public class FootprintAspect {

    private static final String POINTCUT_EXP = "execution(*  com.lht.admin.controller..*.*(..))";

    private static String[] ignoreSuffixArr = {
            ".html",
            ".js",
            ".css",
            ".jpg",
            ".png",
            ".gif"
    };

    /**
     * 日志打印
     */
    @Before(POINTCUT_EXP)
    public void tracer() {

        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) attributes;
        HttpServletRequest request = requestAttributes.getRequest();

        String uri = request.getRequestURI();
        String method = request.getMethod();

        boolean isIgnore = false;
        for (String each : ignoreSuffixArr) {
            if (uri.toLowerCase().endsWith(each)) {
                isIgnore = true;
                break;
            }
        }

        if (!isIgnore) {
            log.info("Function[tracer] url:{},method:{}", uri, method);
        }
    }

}
